package com.work.cos.web.controller.api;

import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.work.cos.domain.entity.AddressEntity;
import com.work.cos.domain.vo.ComServiceResVo;
import com.work.cos.service.interfc.IAddressService;

@RestController
@RequestMapping("/api/address")
public class AddressController {
	
	private static Logger LOGGER = LoggerFactory.getLogger(AddressController.class);
	
	@Autowired
	private IAddressService addressService;

	/**
	 * 查询用户收货地址
	 * @param userId
	 * @param pageNo
	 * @param pageSize
     * @return
     */
	@PostMapping(value = "/query")
	public ComServiceResVo queryAddressList(@RequestParam String userId,
			@RequestParam int pageNo,@RequestParam int pageSize){
		LOGGER.debug("query page address ... ");
		
		return ComServiceResVo.returnList(addressService.findAllAddress(userId, pageNo, pageSize));
	}
	
	/**
	 * 查询用户默认收货地址
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@PostMapping(value = "/default/query")
	public ComServiceResVo queryDefaultAddress(@RequestParam String userId){
		
		AddressEntity entity = addressService.findDefaultAddress(userId);
		
		if(null == entity){
			
			return ComServiceResVo.returnErrorMessage("默认地址未设置!");
		}
		return ComServiceResVo.returnEntity(entity);
	}
	
	/**
	 * 设置默认地址
	 * @param addressId
	 * @param userId
	 * @return
	 */
	@PostMapping(value = "/default/update")
	public ComServiceResVo defaultAddress(@RequestParam Long id,
			@RequestParam String userId){
		
		boolean flag = addressService.setDefaultAddress(id, userId);
		if(! flag){
			
			return ComServiceResVo.returnErrorMessage("设置为默认地址失败!");
		}
		
		return ComServiceResVo.returnSuccessStatus();
	}
	
	/**
	 * 增加收货地址
	 * @param entity
	 * @return
	 */
	@PostMapping(value = "/add")
	public ComServiceResVo addAddress(AddressEntity entity){
		boolean flag = addressService.addAddress(entity);
		if(! flag){
			
			return ComServiceResVo.returnErrorMessage("增加收货地址失败!");
		}
		
		return ComServiceResVo.returnSuccessStatus();
	}
	
	/**
	 * 更新用户地址
	 * @param entity
	 */
	@PostMapping(value = "/update")
	public ComServiceResVo updateAddress(AddressEntity entity){
		
		if(StringUtils.isEmpty(entity.getId())){
			
			return ComServiceResVo.returnErrorMessage("更新主键缺少!");
		}
		
		boolean flag = addressService.updateAddress(entity);
		if(! flag){
			
			return ComServiceResVo.returnErrorMessage("更新用户地址失败!");
		}
		
		return ComServiceResVo.returnSuccessStatus();
	}
	
	
	/**
	 * 删除地址
	 * @param addressId
	 * @param userId
	 * @return
	 */
	@PostMapping(value = "/del")
	public ComServiceResVo delAddress(@RequestParam Long id,
			@RequestParam String userId){
		
		boolean flag = this.addressService.delAddress(id, userId);
		if(! flag){
			
			return ComServiceResVo.returnErrorMessage("删除地址失败!");
		}
		
		return ComServiceResVo.returnSuccessStatus();
	}
}
